package com.sx.shop.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sx.shop.entity.Shop;
import com.sx.shop.entity.User;
import com.sx.shop.mapper.ShopMapper;
import com.sx.shop.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopService {
    //依赖注入
    @Autowired
    private ShopMapper shopMapper;

    public void doSave(Shop shop) {                         //增加和更新
        // TODO Auto-generated method stub
        User user=(User) SecurityUtils.getSubject().getPrincipal();
        shop.setCreateby_id(user.getId());
        if(shop.getId() == null) {//是新增

            shopMapper.insert(shop);
        }else {
            shopMapper.update(shop);
        }

    }

    public void doDel(Integer id) {                         //删除
        // TODO Auto-generated method stub
        shopMapper.delete(id);
    }

    public Shop selectShopById(Integer id) {                //查询
        // TODO Auto-generated method stub
        return shopMapper.selectShopById(id);
    }

    public ResultVo selectShop( String keyword, Integer page , Integer limit) {
        ResultVo vo = new ResultVo();

        PageHelper.startPage( page , limit);//代码下的第一个查询，有分页效果
        List<Shop> list = shopMapper.selectShop(keyword);
        PageInfo<Shop> pageInfo = new PageInfo<>(list);
        vo.setData(list);
        vo.setCount(pageInfo.getTotal());
        return vo;
    }


    public ResultVo selectShopByCreatebyid(String keyword, Integer page , Integer limit,int id) {
        /*return shopMapper.selectShopByCreatebyid(createby_id);*/
        ResultVo vo = new ResultVo();

        PageHelper.startPage( page , limit);//代码下的第一个查询，有分页效果
        List<Shop> list = shopMapper.selectShopByCreatebyid(id,keyword);
        PageInfo<Shop> pageInfo = new PageInfo<>(list);
        vo.setData(list);
        vo.setCount(pageInfo.getTotal());
        return vo;
    }

    public List<Shop> selectShopByCreatebyid(int createby_id){
        return shopMapper.selectShopByCreatebyid(createby_id,null);

    }

}
